import 'package:distribution_mall_flutter/assets.dart';
import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/main.dart';
import 'package:distribution_mall_flutter/pages/home/application/home_service.dart';
import 'package:distribution_mall_flutter/pages/home/home_provider.dart';
import 'package:distribution_mall_flutter/router/route_utils.dart';
import 'package:distribution_mall_flutter/utils/common_third.dart';
import 'package:distribution_mall_flutter/utils/common_widget.dart';
import 'package:distribution_mall_flutter/utils/image_loader.dart';
import 'package:distribution_mall_flutter/utils/storage.dart';
import 'package:distribution_mall_flutter/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:getwidget/shape/gf_button_shape.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:animated_text_kit/animated_text_kit.dart';

/// 左右选择的dialog
class DownHintDialog extends ConsumerWidget {
  final String? leftText;
  final String? rightText;

  const DownHintDialog({super.key, this.leftText, this.rightText});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final appInfoModal = ref.watch(appInfoProvider);
    EPstorage.deviceInfo.get(DeviceInfoBoxKey.patchNumber);
    return Dialog(
      child: Stack(
        children: [
          Positioned(
              top: 5,
              right: 0,
              child: ImageLoader.imageAsset(
                  Assets.assets_icons_home_update_bg_png,
                  hasRoot: false,
                  width: 151,
                  height: 157)),
          Container(
            constraints:
                BoxConstraints(minHeight: appInfoModal.isUpdating ? 180 : 200),
            padding: appInfoModal.isUpdating
                ? const EdgeInsets.fromLTRB(EdgeConstant.edge218, 40, 0, 10)
                : const EdgeInsets.symmetric(
                    horizontal: EdgeConstant.edge218,
                    vertical: EdgeConstant.edge146),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  appInfoModal.isUpdating
                      ? S.current.Updating
                      : '${appInfoModal.titleVersion} ${S.current.update_prompt}',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: const TextStyle(
                      fontSize: 20, color: Color.fromRGBO(29, 27, 32, 1)),
                  textAlign: TextAlign.center,
                ),
                Container(
                  padding: const EdgeInsets.only(
                      top: EdgeConstant.edge146, bottom: EdgeConstant.edge17),
                  constraints: const BoxConstraints(maxHeight: 200),
                  child: SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: appInfoModal.isUpdating
                          ? [
                              DefaultTextStyle(
                                  style: const TextStyle(
                                      color: Color.fromRGBO(73, 69, 79, 1),
                                      fontSize: FontConstant.f146,
                                      letterSpacing: 0.2),
                                  child: AnimatedTextKit(
                                    animatedTexts: [
                                      WavyAnimatedText(
                                          S.current.it_will_shortly),
                                    ],
                                    isRepeatingAnimation: true,
                                  ))
                            ]
                          : (appInfoModal.data?.content
                                  ?.split('/n')
                                  .map((item) => Text(
                                        item,
                                        style: const TextStyle(
                                            color:
                                                Color.fromRGBO(73, 69, 79, 1),
                                            fontSize: FontConstant.f146,
                                            letterSpacing: 0.2),
                                      ))
                                  .toList() ??
                              []),
                    ),
                  ),
                ),
                Visibility(
                    visible: appInfoModal.isUpdating,
                    child: LoadingAnimationWidget.staggeredDotsWave(
                        color: const Color.fromRGBO(30, 135, 250, 1),
                        size: 40)),
                Visibility(
                    visible: appInfoModal.isUpdating != true,
                    child: Container(
                      margin: const EdgeInsets.only(top: 24),
                      height: 40,
                      child: Row(
                        children: [
                          Visibility(
                              visible: appInfoModal.hideCancel == false,
                              child: CommonBtnWidget.generalBtn(() {
                                ref.read(homeServiceProvider).cancel();
                                // RouteUtils.back();
                                SmartDialog.dismiss();
                              },
                                  border: Border.all(
                                      color: Colors.black12, width: 0.9),
                                  color: Colors.white,
                                  height: 42.8,
                                  width: 80,
                                  shape: GFButtonShape.square,
                                  text: S.current.cancel,
                                  textStyle: const TextStyle(
                                      color: Colors.black26, fontSize: 14))),
                          const SizedBox(
                            width: 10,
                          ),
                          Expanded(
                              child: CommonBtnWidget.generalBtn(() {
                            ref.read(homeServiceProvider).confirm();
                          },
                                  text: appInfoModal.confirmText,
                                  color: const Color.fromRGBO(30, 135, 250, 1),
                                  height: 43.7,
                                  fullWidthButton: true,
                                  shape: GFButtonShape.square,
                                  textStyle: const TextStyle(
                                      fontWeight: FontWeight.w500,
                                      color: Colors.white,
                                      fontSize: FontConstant.f16_4))),
                        ],
                      ),
                    )),
              ],
            ),
          )
        ],
      ),
    );
  }
}
